package responese

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/testdata/protoexample"
	"net/http"
)

func init() {
	r := gin.Default()
	// json
	r.GET("/json", func(context *gin.Context) {
		context.JSON(http.StatusOK, gin.H{"message": "JSON", "Code": 200})
	})
	// struct响应
	r.GET("/struct_valid", func(context *gin.Context) {
		var msg struct {
			Name    string
			Message string
			Number  int
		}
		msg.Name = "root"
		msg.Message = "message"
		msg.Number = 123
		context.JSON(http.StatusOK, msg)
	})
	// XML
	r.GET("/xml", func(context *gin.Context) {
		context.XML(http.StatusOK, gin.H{
			"message": "abc",
		})
	})
	// YAML
	r.GET("/yaml", func(context *gin.Context) {
		context.YAML(http.StatusOK, gin.H{
			"name": "zhangsan",
		})
	})
	// protobuf
	r.GET("protobuf", func(context *gin.Context) {
		reps := []int64{int64(1), int64(2)}
		label := "label"
		data := &protoexample.Test{
			Label: &label,
			Reps:  reps,
		}
		context.ProtoBuf(http.StatusOK, data)
	})
	r.Run(":8000")
}
